<?php
namespace app\home\controller;
use app\home\controller;
use app\home\model\CheckStandardItemM;
use app\home\model\CheckStandardM;
use myResponse\Res;
use think\Db;
use think\Request;

//设备分类控制器
class CheckStandard extends Base{
    //显示分类列表

    function getList($page = 1,$size = 10,$StandardTypeId = ''){
        $page *=1;
        $size *=1;
        $m= new CheckStandardM;
        $where='';
        if($StandardTypeId && "undefined"!=$StandardTypeId){
            $where['StandardTypeId'] = $StandardTypeId;
        }
        $z=$m->listP($page,$size,$where);
        Res::suc('ok',$z);
    }

    //添加设备分类
    function addOne(){
        //在CheckStandardModel里边通过一个指定方法实现权限添加
        $m= new CheckStandardM;
        $this->ret($m->addOne(),0);
    }
    //修改设备分类
    function editOne(){
        //在CheckStandardModel里边通过一个指定方法实现权限添加
        $m= new CheckStandardM;
        $this->ret($m->editOne(),1);

    }
    //复制分类
    function copyOne(){
        //在CheckStandardModel里边通过一个指定方法实现权限添加
        $data = input('post.');
        unset($data['StandardId']);
        $data['Creator'] = session('user.UserId');
        $m = new CheckStandardM();
        // 过滤post数组中的非数据表字段数据
        $z = $m->allowField(true)->save($data);
        $id =$m->StandardId;
        $this->addMore($id);
        $this->ret($id,3);

    }
    public function addMore($id=1){
        $it = new CheckStandardItemM();
//        $mp['StandardId'] = input('post.StandardId');
        $mp['StandardId'] = 1;
        $item = $it->where($mp)->select();
        $data=[];
        foreach ($item as $k => $v){
            $i['StandardId']=$id;
            $i['CheckPosition']=$v['CheckPosition'];
            $i['CheckContent']=$v['CheckContent'];
            $i['Param']=$v['Param'];
            $i['CheckJob']=$v['CheckJob'];
            $i['CycleTypeId']=$v['CycleTypeId'];
            $i['Sort']=$v['Sort'];
            $i['Creator']=$v['Creator'];
            $data[$k]=$i;
        }
        $res = $it->strict(false)->insertAll($data);
//        df($res);
    }
    //删除设备分类
    function delOne(){
        $m= new CheckStandardM;
        $this->ret($m->delOne(),2);
    }

    function ret($z,$tp){
        $ret=['添加','修改','删除','复制'];
        if($z)
            Res::suc($ret[$tp].'成功！');
        else
            Res::errs($ret[$tp].'失败！');
    }
}
